#include "stdafx.h"
#include "PKCSSignedData.h"

namespace AppliedCryptography
{
	PKCSSignedData::PKCSSignedData()
		: _oid(new ASN1ObjectIdentifier()),
		_content(new ASN1Sequence()),
		_cg(new CertificateGenerator()),
		_cmsVers(new ASN1Integer()),
		_digestAlgorithms(new ASN1Set()),
		_encapContentInfo(new ASN1Sequence()),
		_signerInfos(new ASN1Set()),
		_encapsContentType(new ASN1ObjectIdentifier()),
		_certificates(new PKCSCertificateSet())
	{
		_oid->set("1.2.840.113549.1.7.2");
		_cmsVers->set(1);
		_encapsContentType->set("1.2.840.113549.1.7.1");
	};

	PKCSSignedData::~PKCSSignedData()
	{
		delete _oid;
		delete _content;
		delete _cmsVers;
		delete _digestAlgorithms;
		delete _encapContentInfo;
		delete _signerInfos;
		delete _encapsContentType;
		delete _certificates;
	};

	ASN1ObjectIdentifier& PKCSSignedData::getPKCSContentType() const
	{
		return *_oid;
	};

	ASN1Encodable& PKCSSignedData::getPKCSContent() const
	{
		//Explicit00 certificates 

		_content->flush();

		_encapContentInfo->flush();
		_encapContentInfo->add(*_encapsContentType);

		_certificates->flush();
		_certificates->add(*_cg);

		_content->add(*_cmsVers);
		_content->add(*_digestAlgorithms);
		_content->add(*_encapContentInfo);
		_content->add(*_certificates);
		_content->add(*_signerInfos);

		return *_content;
	};

	PKCSSignedData& PKCSSignedData::setCertificate(CertificateGenerator& cg)
	{
		_cg = &cg;
		return *this;
	};

	CertificateGenerator& PKCSSignedData::getCertificate() const
	{
		return *_cg;
	};
}; // namespace AppliedCryptography
